import { pgTable, uuid, index, unique } from 'drizzle-orm/pg-core';
import { sql } from 'drizzle-orm';
import { simpleAuditFields } from './audit-fields';

/**
 * 系统-用户岗位关联表
 */
export const sysUserPost = pgTable(
  'sys_user_post',
  {
    /** 关联ID（主键） */
    id: uuid('id').primaryKey().default(sql`uuidv7()`),
    /** 用户ID（逻辑外键，关联 sys_user.id） */
    userId: uuid('user_id').notNull(),
    /** 岗位ID（逻辑外键，关联 sys_post.id） */
    postId: uuid('post_id').notNull(),
    ...simpleAuditFields(),
  },
  (table) => [
    unique('sys_user_post_user_id_post_id_key').on(table.userId, table.postId),
    index('sys_user_post_user_id_idx').on(table.userId),
    index('sys_user_post_post_id_idx').on(table.postId),
  ]
);

export type SysUserPost = typeof sysUserPost.$inferSelect;
export type NewSysUserPost = typeof sysUserPost.$inferInsert;

